<template>
  <div class="content">
    <el-row>
      <el-col :span="5">
        分组：
        <el-select v-model="status" placeholder="请选择分组" @change="getList">
          <el-option v-for="item in groupname_list" :key="item.value" :label="item.groupname" :value="item.groupname"></el-option>
        </el-select>
      </el-col>
    </el-row>
    <div class="table">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="vipid" label="vipid" align="center"></el-table-column>
        <el-table-column width="180" prop="uname" label="用户名" align="center"></el-table-column>
        <el-table-column width="180" prop="realname" label="昵称" align="center"></el-table-column>
        <el-table-column width="180" prop="balance" label="余额" align="center"></el-table-column>
        <el-table-column width="180" prop="mobile" label="电话" align="center"></el-table-column>
        <el-table-column width="180" prop="qq" label="QQ" align="center"></el-table-column>
        <el-table-column width="200" prop="email" label="邮箱" align="center"></el-table-column>
        <el-table-column width="200" prop="lastLoginIp" label="上次登录IP" align="center"></el-table-column>
        <el-table-column width="200" prop="lastLoginTime" label="上次登录时间" align="center"></el-table-column>
        <el-table-column width="200" prop="regyime" label="注册时间" align="center"></el-table-column>
        <el-table-column width="150" prop="groupname" label="组名" align="center"></el-table-column>
        <el-table-column fixed="right" v-if="$_permissions()=='2'||$_permissions()=='3'" label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row, true)" type="text" size="small">编辑</el-button>
            <el-button type="text" size="small" @click="handleClick(scope.row, false)" v-if="$_permissions()=='3'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.start"
      :page-sizes="[10, 25, 50, 100]"
      :page-size="pagination.nums"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
    ></el-pagination>
    <el-dialog :title="ruleForm.vipid ? '编辑' : '添加'" :visible.sync="show" width="30%" @close="handleClose">
      <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="vipid" prop="vipid"><el-input :readonly="true" v-model="ruleForm.vipid"></el-input></el-form-item>
          <el-form-item label="用户名" prop="uname"><el-input v-model="ruleForm.uname"></el-input></el-form-item>
          <el-form-item label="昵称" prop="realname"><el-input v-model="ruleForm.realname"></el-input></el-form-item>
          <el-form-item label="密码" prop="upsw"><el-input v-model="ruleForm.upsw"></el-input></el-form-item>
          <el-form-item label="余额" prop="balance"><el-input v-model="ruleForm.balance"></el-input></el-form-item>
          <el-form-item label="电话" prop="mobile"><el-input v-model="ruleForm.mobile"></el-input></el-form-item>
          <el-form-item label="QQ" prop="qq"><el-input v-model="ruleForm.qq"></el-input></el-form-item>
          <el-form-item label="邮箱" prop="email"><el-input v-model="ruleForm.email"></el-input></el-form-item>
          <el-form-item label="组名">
            <el-select v-model="ruleForm.groupname" placeholder="请选择" @change="changeGroupname($event, ruleForm.vipid)">
              <el-option v-for="(item, index) in groupname_list" :label="item.groupname" :value="item.groupname"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="上次登录IP" prop="lastLoginIp"><el-input :readonly="true" v-model="ruleForm.lastLoginIp"></el-input></el-form-item>
          <el-form-item label="上次登录时间" prop="lastLoginTime"><el-input :readonly="true" v-model="ruleForm.lastLoginTime"></el-input></el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">{{ ruleForm.vipid ? '保存' : '创建' }}</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchFZuser,showFZ,showALLluser, updatetusersFZ, deluser, updateuser } from '@/api/test.js';
import pagination from '@/mixins/page.js';
export default {
  name: 'hongcaitj',
  mixins: [pagination],
  data() {
    return {
      tableData: [],
      show: false,
      groupname_list: [],
      status:'',
      ruleForm:{},
      rules:{}
    };
  },
  methods: {
    handleClick(row, status) {
      //status true:编辑 false:删除
      if (status) {
        //编辑
        this.ruleForm = { ...row };
        this.show = true;
      } else {
        //删除
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            deluser({ vipid: row.vipid }).then(res => {
              console.log(res);
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success('删除成功');
                this.getList();
              } else {
                this.$message.error('删除失败');
              }
            });
          })
          .catch(() => {});
      }
    },
    submitForm() {
      //提交请求
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          if (this.ruleForm.vipid) {
            //更新
            updateuser({...this.ruleForm,...{
              name:this.ruleForm.uname,
              psw:this.ruleForm.upsw
            }}).then(res => {
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success(res.msg);
                this.handleClose();
                this.getList();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            //添加
            insertFZ(this.ruleForm).then(res => {
              if (res.msg.indexOf('成功') != -1) {
                this.$message.success(res.msg);
                this.handleClose();
                this.getList();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
    getList() {
      //获取列表
      searchFZuser({...this.pagination,...{
        groupname:this.status
      }}).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.pagination.total = res.total;
      });
    },
    getshowFZ() {
      //获取分组列表
      showFZ({
        nums: 100,
        start: 1
      }).then(res => {
        this.groupname_list = res.data;
      });
    },
    handleClose() {
      //弹窗关闭回调 -- 清空表单数据
      this.ruleForm = {};
      this.show = false;
      this.$refs['ruleForm'].resetFields();
    },
  },
  mounted() {
    this.getList();
    this.getshowFZ();

  }
};
</script>

<style lang="scss" scoped>
.content {
}
</style>
